package com.qian.ddj.pay.util;

import com.qian.api.ddj.util.RSA;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Map;

public class PaixuSign {
   public  static String getSign(Map<String, String> map, String key){
        //排序　　签名　
        StringBuilder sb= new StringBuilder();
        String[] pams= new String[0];
        pams=		map.keySet().toArray(pams);
        Arrays.sort(pams);
        for(int i=0; i<pams.length;i++){
            if(null != map.get(pams[i]) && map.get(pams[i]) != ""){
                sb.append(pams[i]).append("=").append(map.get(pams[i])).append("&");
            }
        }
        sb.setLength(sb.length()-1);
        System.out.println("请求报文："+sb);
        String sign = "";
        if (key.length() > 32) {
            //rsa
            try {
                sign = RSA.sign(sb.toString(), key);
                //System.out.println("\n sign:"+sign);
                sign = URLEncoder.encode(sign, "UTF-8");
                //System.out.println("\n sign:"+sign);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            //md5
            //签名生成sign
            System.out.println("\n sign:" + sign);
        }

        return sign;

    }
}
